#ifndef XGE_GRAPHICS_H_
#define XGE_GRAPHICS_H_
#include <windows.h>
#include "g_colors.h"
#include "g_common.h"
#include "xtype.h"
namespace xge{

namespace window{
  class GraphicsWindow;
}

namespace graphics{

  class Texture;
  class RenderTexture;
  class RenderTarget;

  // This class provide very simply and clear 2D rendering interface
  // for doing the rendering using Direct3D or OpenGL technique.
  // The design of this renderer engine is typically based on simple rules(KISS).
  // Every additional function is omitted , it only provide the most
  // basic and fundamental function. More feature is designed by the
  // upper class instead of this class .

  class Graphics2D {
  public:
    enum {
      MODE_NORMAL , // normal mode for rendering
      MODE_BATCH ,  // batching mode for rendering
      PRIMITIVE_QUAD,
      PRIMITIVE_LINE
    };
  public: //================Texture interface
    // Create the texture from the raw buffer ( this buffer should be loaded
    // directly from file ).
    // Color
    virtual Texture* CreateTexture( void* raw_buffer , size_t size , const Color& color_key ) = 0;
    // Create the texture manually
    virtual Texture* CreateTexture( size_t width , size_t height ) = 0;
    // Destroy the texture 
    virtual bool DestroyTexture( Texture* texture ) = 0;
  public: //================Render target interface
    // Create render-target 
    virtual RenderTarget* CreateRenderTarget( size_t width , size_t height ) = 0;
    // Destroy the render target
    virtual bool DestroyRenderTarget( RenderTarget* target ) =0;
  public:
    // two rendering function 
    virtual void RenderQuad( const Quad& quad ) =0;
    virtual void RenderLine( const Point& p1,const Point& p2,float zbuffer , int blending , const Color& color1, const Color& color2 ) = 0;
    // Starts the scene
    // If the render target has been created and vertex buffer 
    // has been specified , the BeginScene will succeed , 
    // otherwise it will return the error code.
    virtual int BeginScene(
      int mode = MODE_NORMAL ,
      int primitive_type = PRIMITIVE_QUAD,
      RenderTarget* target = NULL,
      Texture* texture= NULL,
      int blending_mode= BLEND_DEFAULT,
      size_t wait_time_out = 100  // this parameter is used to indicate when the device lost , wait 100ms until it finish the lost status
      ) =0 ;
    virtual int EndScene( bool sync = false ) = 0;
    virtual void Clear( const Color& color ) =0;
  public:
    virtual void SetClippingArea( const Rect& rect ) = 0;
    virtual void SetGlobalTransformation( float an_x = 0.0f ,float an_y = 0.0f  ,
      float dis_x = 0.0f  ,float dis_y = 0.0f , 
      float horizontal_scale = 0.0f  , float vertical_scale=  0.0f  , float rotation=  0.0f  ) =0;
    // Get the specific graphics window binded to this class.
    virtual window::GraphicsWindow* GetGraphicsWindow() = 0;
  public:
    virtual ~Graphics2D() {}
  };

  // In fact , the CreateGraphics2device will really created the
  // interface only once ,if you calling it again , it will return
  // the previous interface you've obtained before.You can only obtain
  // one instance of Graphics2DDeivce in one process .
  // Creating a default Graphics2D interface by specifying 
  // the input parameter .
  Graphics2D* CreateGraphics2DDevice( 
    int* error_code , // if there exists an error , the error code will return the error value 
    size_t width , size_t height ,
    bool fullscreen ,
    const String& caption ,
    const String& icon_res_string = String() , // icon resource string 
    DeviceType type = DEVICE_DEFAULT ); // the rendering device type , now only support Direct3D8
  // Destroy the the graphics2D device 
  bool DestroyGraphics2DDevice( Graphics2D* g );

}// namespace graphics
}// namespace xge

#endif// XGE_GRAPHICS_INTERFACE_H_